<?php

namespace esp\core;

final class Request
{
    private $_var = [];
    public $loop = false;//控制器间跳转循环标识
    public $route = null;
    public $params = [];

    public $module;
    public $controller;
    public $action;
    public $method;
    public $directory;
    public $referer;
    public $uri;

    public function __construct()
    {
//        $this->module = Config::system('frame.esp.module');
//        $this->controller = Config::system('frame.esp.controller');
//        $this->action = Config::system('frame.esp.action');

        $this->method = strtoupper(getenv('REQUEST_METHOD'));
        $this->directory = root(Config::system('frame.esp.directory'));
        $this->referer = _CLI ? null : (getenv("HTTP_REFERER") ?: '');
        $this->uri = _CLI ? //CLI模式下 取参数作为路由
            ('/' . trim(implode('/', array_slice($GLOBALS["argv"], 1)), '/')) :
            parse_url(getenv('REQUEST_URI'), PHP_URL_PATH);
    }

    public function __get(string $name)
    {
        return isset($this->_var[$name]) ? $this->_var[$name] : null;
    }

    public function get(string $name)
    {
        return isset($this->_var[$name]) ? $this->_var[$name] : null;
    }

    public function __set(string $name, $value)
    {
        $this->_var[$name] = $value;
    }

    public function set(string $name, $value)
    {
        $this->_var[$name] = $value;
    }

    public function getParams()
    {
        unset($this->params['_plugin_debug']);
        return $this->params;
    }

    public function getParam(string $key)
    {
        return isset($this->params[$key]) ? $this->params[$key] : null;
    }

    public function setParam(string $name, $value)
    {
        $this->params[$name] = $value;
    }

    public function getMethod()
    {
        return $this->method;
    }

    public function isGet()
    {
        return $this->method === 'GET';
    }

    public function isPost()
    {
        return $this->method === 'POST';
    }

    public function isCli()
    {
        return $this->method === 'CLI';
    }

    public function isAjax()
    {
        return _CLI ? false : strtolower(getenv('HTTP_X_REQUESTED_WITH')) === 'xmlhttprequest';
    }

    public function agent()
    {
        return getenv('HTTP_USER_AGENT') ?: '';
    }

    /**
     * 是否手机访问
     * 0=不是
     * 1=是手机
     * 2=是微信
     * @return bool
     */
    public function isWap()
    {
        $browser = $_SERVER['HTTP_USER_AGENT'] ?? '';
        if (empty($browser)) return 0;
        if (strripos($browser, "MicroMessenger")) return 2;//微信
        if (stripos($browser, "mobile") || stripos($browser, "android")) return 1;
        if (isset($_SERVER['HTTP_VIA']) or isset($_SERVER['HTTP_X_NOKIA_CONNECTION_MODE']) or isset($_SERVER['HTTP_X_UP_CALLING_LINE_ID'])) return 1;
        if (stripos(strtoupper($_SERVER['HTTP_ACCEPT'] ?? ''), "VND.WAP.WML") > 0) return 1;
        $browser = substr($browser, 0, 4);
        $mobs = ['Noki', 'Eric', 'WapI', 'MC21', 'AUR ', 'R380', 'UP.B', 'WinW', 'UPG1', 'upsi', 'QWAP', 'Jigs', 'Java', 'Alca', 'MITS', 'MOT-', 'My S', 'WAPJ', 'fetc', 'ALAV', 'Wapa', 'Oper'];
        return in_array($browser, $mobs) ? 1 : 0;
    }

}